package com.zim.ta_service.base.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * Double类型序列化 保留最多2位小数
 * author: alabimofa <br>
 * date: 2021/11/12 17:57 <br>
 */
public class DoubleSerialize extends JsonSerializer<Double> {

	private final DecimalFormat df = new DecimalFormat("0.##");

	@Override
	public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		if (null != value) {
			gen.writeString(df.format(value));
		}
	}
}
